﻿Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Public Class PW
'    Public Shared Property fs As New FileStream(file, FileMode.OpenOrCreate)

    Public Shared Property  Users As New list(Of UserInfo)()

    Public Shared Property  LastUser As  UserInfo

    Public Shared Property User As String
    Public Shared Property  Passwd As String 

    Public Shared Property file as String =Path.Combine(Application.StartupPath,"data.bin")

    Public Shared Sub SavePw(ByVal us As String, byval pw As string, byval lmt As integer,byval auto As integer)
        '保存用户和密码
'        Dim newcount as Integer =1
      


        dim  newcount as Integer =Users.count+1
        
        Dim userInfo2 As New UserInfo(newcount,us,pw,lmt)

        Dim fs As FileStream

        ' 登录时 如果没有Data.bin文件就创建、有就打开
        Try
            fs =New FileStream(file, FileMode.OpenOrCreate)

        Catch ex As Exception
            exit sub
        End Try
  
        Dim bf As New BinaryFormatter()
        ' 保存用户名
'
'        userInfo2.LoginID = us
'        '保存密码
'        userInfo2.Pwd =pw
      
        '选在集合中是否存在用户名 
        
        
        Dim userfind= Users.Find(Function(a) a.LoginID=userInfo2.LoginID)
        If  userfind IsNot nothing Then
            '如果有,更新密码
            If userfind.Pwd<>userInfo2.Pwd Then
                userfind.Pwd=userInfo2.Pwd
            end if
            If userfind.LtmPro<>userInfo2.LtmPro Then
                userfind.LtmPro=userInfo2.LtmPro
            end if
            If userfind.IFauto<>userInfo2.IFauto Then
                userfind.IFauto=userInfo2.IFauto
            end if
        Else 
            '如果没有，则新增
            Users.Add(userInfo2)

        End If
     

        '添加用户信息到集合

'        Users.Add(userInfo2.LoginID, userInfo)

        '最新使用的账户

        Users.insert(0,LastUser)
'        Users(0).ID=0
'        If Users.Count=1 Then
'            Users.insert(0,LastUser)
'        Else 
'            Users(0)=LastUser
'            Users(0).ID=0
'        End If
        

        '写入文件
        bf.Serialize(fs, Users)
        '关闭
        fs.Close()
    End Sub

    Public  Shared Sub OpenPw()
        '打开用户和密码

        Dim fs As New FileStream(file, FileMode.OpenOrCreate)
        'fs.Seek(0, SeekOrigin.Begin);

        If fs.Length > 0 Then
            Dim bf As New BinaryFormatter()
            fs.Position = 0
            '读出存在Data.bin 里的用户信息
            Users = TryCast(bf.Deserialize(fs), list(Of UserInfo))
        End If
        fs.Close()

        if Users.Count=0 Then
            Users=New List(Of UserInfo)()
            LastUser=new UserInfo()
'            Users.Add(New UserInfo(0,0,0,0,0))
        Else 
            LastUser=new UserInfo(Users.Item(0))
            Users.RemoveAt(0)
            
        End If

'        LastUser=new UserInfo(Users.Item(0))

        

    End Sub

    Public  Shared Sub RecordLastPw(ByVal us As String, byval pw As string,ByVal lmtpro As integer,ByVal auto As integer)

        LastUser=new UserInfo()
        LastUser.ID=0
        lastUser.LoginID=us
        LastUser.Pwd=pw
        LastUser.LtmPro=lmtpro
        LastUser.IFauto=auto
    End Sub


End Class